﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.BLL;
using System.Web.Script.Serialization;
using Register.Model;

namespace RegisterWeb.Manager.SystemUserManager.ashx
{
    /// <summary>
    /// UserRoleService 的摘要说明
    /// </summary>
    public class UserRoleService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
             string action=  context.Request["action"];
               action = action;
               if (action == "get")
               {
                   //int intPageSize = int.Parse(context.Request["rows"].ToString());
                   //int intCurrentPage = int.Parse(context.Request["page"].ToString());
                   string id = context.Request["userID"].ToString();
                   var userrolelist=  UserRoleBLL.GetList(id);
                   var list = RoleBLL.GetAllRole("1=1", 1, 1000);
                   foreach (var row in list)
                   {
                       row.Role_int = 0;
                   }
                   for (int i = 0; i < list.Count; i++)
                   {
                       for (int j = 0; j < userrolelist.Count; j++)
                       {
                           if (list[i].Role_ID == userrolelist[j].Role_ID)
                           {
                               list[i].Role_int = 1;
                           }
                       }
                   }
                   //foreach (var row in list)
                   //{ 
                   //  foreach(var row1 in userrolelist)
                   //    {
                   //        if (row.Role_int == 0)
                   //         {
                   //             if (row.Role_ID.Equals(row1.Role_ID))
                   //             {
                   //                 row.Role_int =1;
                   //             }
                   //        }
                       
                   //    }
                   //}
                   JavaScriptSerializer jss = new JavaScriptSerializer();
                   context.Response.Write(jss.Serialize(list));
               }
               else if (action == "add") 
               {
                   UserRole userrole=new UserRole();
                 userrole.Role_ID=context.Request["Role_ID"].ToString();
                 userrole.Administrator_ID= context.Request["Administrator_ID"].ToString();
                 userrole.UserRole_Describe = "";
                 userrole.UserRole_Addtime = DateTime.Now;
                 userrole.UserRole_Recoder = "1";
                 userrole.UserRole_LastAmendTime = DateTime.Now;
                 userrole.UserRole_LastAmendPerson = "1";
             if( UserRoleBLL.Add(userrole)>0)
                 {
                     context.Response.Write("ok");
                 }
                 else
                 {
                     context.Response.Write("no");
                 }
               }
               else if (action == "delete")
               {
                string Role_ID=context.Request["Role_ID"].ToString();
                string  Administrator_ID= context.Request["Administrator_ID"].ToString();
                if (UserRoleBLL.Delete(Role_ID, Administrator_ID))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}